/**
 *
 */
package com.xuxingcan.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import org.apache.log4j.Logger;

import com.android.ddmlib.IDevice;
import com.xuxingcan.device.ADB;
import com.xuxingcan.minicap.AndroidScreenObserver;
import com.xuxingcan.minicap.MiniCapUtil;

/**
 * @author hui.qian qianhui@58.com
 * @date 2015年8月14日 下午7:40:17
 */

public class MinicapTest extends JFrame {
    private static final Logger LOG = Logger.getLogger("PageTest.class");

    public static final int imgWidth = 1080 / 3;
    public static final int imgHeight = 1920 / 3;
    private int screenWidth = imgWidth + 16;
    private int screenHeight = imgHeight + 36;

    public MinicapTest() {
        ADB adb = new ADB();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((dim.width - this.getWidth()) / 2, 0);
        this.setMinimumSize(new Dimension(screenWidth, screenHeight));
        this.setVisible(true);
        setLayout(new GridLayout());
        if (adb.getDevices().length <= 0) {
            LOG.error("无连接设备,请检查");
            return;
        }
        IDevice[] devicelist = adb.getDevices();
        for (IDevice device : devicelist) {
//            IDevice device = adb.getDevices()[0];
            MyPanel mp = new MyPanel(device, this);
            this.getContentPane().add(mp);
        }
        pack();
    }

    public static void main(String[] args) {
        new MinicapTest();
    }

    class MyPanel extends JPanel implements AndroidScreenObserver {

        BufferedImage image = null;
        MiniCapUtil minicap = null;

        public MyPanel(IDevice device, MinicapTest frame) {
            minicap = new MiniCapUtil(device);
            minicap.registerObserver(this);
            minicap.takeScreenShotOnce();
            minicap.startScreenListener();
            setSize(imgWidth, imgHeight);
            addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent e) {
//                    System.out.println("mouseClicked");
                }

                @Override
                public void mousePressed(MouseEvent e) {
//                    System.out.println("mousePressed    " + e.getX() + "," + e.getY());
                    minicap.ExecuteTouchDown(e.getX(), e.getY());
                }

                @Override
                public void mouseReleased(MouseEvent e) {
//                    System.out.println("mouseReleased    " + e.getX() + "," + e.getY());
                    minicap.ExecuteTouchUp(e.getX(), e.getY());
                }

                @Override
                public void mouseEntered(MouseEvent e) {
//                    System.out.println("mouseEntered");
                }

                @Override
                public void mouseExited(MouseEvent e) {
//                    System.out.println("mouseExited");
                }
            });
            addMouseMotionListener(new MouseMotionListener() {
                @Override
                public void mouseDragged(MouseEvent e) {
//                    System.out.println("mouseDragged    " + e.getX() + "," + e.getY());
                    minicap.ExecuteTouchMove(e.getX(), e.getY());
                }

                @Override
                public void mouseMoved(MouseEvent e) {
//                    System.out.println("mouseMoved");
                }
            });
        }

        @Override
        public void paint(Graphics g) {
            try {
                if (image == null)
                    return;
//                MinicapTest.this.setMinimumSize(new Dimension(screenWidth, screenHeight));
                g.drawImage(image, 0, 0, imgWidth, imgHeight, null);
                setSize(imgWidth, imgHeight);
                image.flush();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void frameImageChange(Image image) {
            this.image = (BufferedImage) image;
            int w = this.image.getWidth();
            int h = this.image.getHeight();
////            float radio = (float) imgWidth / (float) w;
////            imgHeight = (int) (radio * h);
            System.out.println("imgWidth : " + w + ",imgHeight : " + h);
            this.repaint();
        }
    }

}
